package com.hbm.usercenter.fragment;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;

import com.hbm.usercenter.MainActivity;
import com.hbm.usercenter.R;
import com.hbm.usercenter.base.BaseFragment;
import com.hbm.usercenter.base.LoadingPager.LoadedResult;
import com.hbm.usercenter.bean.UserInfoBean;
import com.hbm.usercenter.conf.Constants;
import com.hbm.usercenter.http.MyCallBack;
import com.hbm.usercenter.http.NetClient;
import com.hbm.usercenter.protocol.MyInfoProtocol;
import com.hbm.usercenter.utils.CommonUtils;
import com.hbm.usercenter.utils.LogUtils;
import com.hbm.usercenter.utils.UIUtils;
import com.hbm.usercenter.widget.GroupInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class UpdateInfoFragment extends BaseFragment {
    @Bind(R.id.gp_name)
    GroupInfo mGpName;
    @Bind(R.id.gp_bankcard)
    GroupInfo mGpBankcard;
    @Bind(R.id.gp_account_name)
    GroupInfo mGpAccountName;
    //
    private MyInfoProtocol mProtocol;
    private UserInfoBean mData;

    @Override
    protected LoadedResult initData() {
        mProtocol = new MyInfoProtocol();
        try {
            mData = mProtocol.loadData(0);
            LogUtils.s(mData.toString());
            LoadedResult checkState = checkState(mData);
            return checkState;
        } catch (Exception e) {
            e.printStackTrace();
            return LoadedResult.ERROR;
        }
    }

    @Override
    protected View initSuccessView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_update_info, null);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        UserInfoBean.UserinfoBean userinfo = mData.getUserinfo();
        mGpName.setText(userinfo.getUser_name());

        UserInfoBean.WithdrawinfoBean withdrawinfo = mData.getWithdrawinfo();
        mGpAccountName.setText(withdrawinfo.getAccount_name());
        mGpBankcard.setText(withdrawinfo.getBank_card());
    }

    @OnClick(R.id.btn_update)
    public void onViewClicked() {
        Constants.isCache=false;
        sendRequest();
    }
    private void sendRequest() {
        String user_name = mGpName.getText();
        String bank_card = mGpBankcard.getText();
        String account_name = mGpAccountName.getText();
        Map<String, String> params = new HashMap<String, String>();
        params.put("user_name", user_name);
        params.put("bank_card", bank_card);
        params.put("account_name", account_name);
        NetClient.getNetClient().callNet(Constants.URLS.UPDATEINFO, new MyCallBack() {
            @Override
            public void onFailure(IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(String json) {
                processResult(json);
                LogUtils.d(json);

            }
        }, params);

    }

    private void processResult(String json) {
        JSONObject rootObject = null;
        try {
            rootObject = new JSONObject(json);
            final String err_msg = rootObject.getString("err_msg");
            String err_code = rootObject.getString("err_code");
            if (err_code.equals("-1")) {
                UIUtils.postTaskSafely(new Runnable() {
                    @Override
                    public void run() {
                        CommonUtils.showShort(UIUtils.getContext(), err_msg);
                    }
                });
                //                Intent intent = new Intent(getActivity(), QRCodeActivity.class);
                //                intent.putExtra("money", number);
                //                //                intent.putExtra("money", money);
                //                //                intent.putExtra("picUrl", picUrl);
                //                //
                //                startActivity(intent);
                startActivity(new Intent(getActivity(), MainActivity.class));
            } else {
                UIUtils.postTaskSafely(new Runnable() {
                    @Override
                    public void run() {
                        CommonUtils.showShort(UIUtils.getContext(), err_msg);
                    }
                });
                //                Toast.makeText(UIUtils.getContext(), err_msg, Toast
                // .LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

}
